package com.atguigu.gmall.common.config.exception.handler;

import com.atguigu.gmall.common.execption.GmallException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.atguigu.gmall.common.result.Result;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-10 19:19
 **/
@Slf4j
@RestControllerAdvice // advice表示我是一个通知，是所有controller的切面。统一处理异常
public class GlobalExceptionHandler {
    /**
     * 业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(GmallException.class)
    public Result handleGmallException(GmallException e) {
        log.info("错误:{}",e);
        Result<Object> fail = Result.fail();
        fail.setCode(e.getCode());
        fail.setMessage(e.getMessage());
        return fail;
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.info("错误:{}",e);
        Result<Object> fail = Result.fail();
        fail.setMessage(e.getMessage());
        return fail;
    }
}
